package com.wutianyi.module.commons;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.wutianyi.utils.Utils;

/**
 * @author hanjiewu
 * 
 */
public class Item implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6412819397706844866L;
	
	private transient int id;
	private int gId;
	private int order;
	
	private String name;
	private String description;
	
	private Date gmtCreate;
	private Date gmtModified;
	
	private String value;
	private transient List<Pair> properties;
	
	private transient Map<Integer, Pair> mapProperties;
	private transient int hashCode = -1;
	
	public Item(int gId, String name, String value, String description, int order) {
		this.gId = gId;
		this.name = name;
		this.value = value;
		this.description = description;
		this.order = order;
		gmtCreate = new Date();
		gmtModified = new Date();
	}
	
	public Item(int gId, String name, String value, String description, int order,
			Map<String, String> pValues) {
		this(gId, name, value, description, order);
		if (null != pValues) {
			properties = new ArrayList<Pair>();
			for (Entry<String, String> entry : pValues.entrySet()) {
				properties.add(new Pair(entry.getKey(), entry.getValue()));
			}
		}
	}
	
	public Item() {
		gmtCreate = new Date();
		gmtModified = new Date();
	}
	
	public void setValue(String value) {
		this.value = value;
	}
	
	public Object getValue() {
		return value;
	}
	
	public void addProperty(String name, String value) {
		checkProperties();
		properties.add(new Pair(name, value));
	}
	
	public void addProperty(Pair pair) {
		if (null == mapProperties) {
			mapProperties = new HashMap<Integer, Pair>();
		}
		checkProperties();
		properties.add(pair);
		mapProperties.put(pair.getId(), pair);
	}
	
	public Pair getProperty(int id) {
		if (null == mapProperties) {
			return null;
		}
		return mapProperties.get(id);
	}
	
	// public Object getProperty(String name) {
	// checkProperties();
	// return properties.get(name);
	// }
	
	private void checkProperties() {
		if (null == properties) {
			properties = new ArrayList<Pair>();
		}
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getgId() {
		return gId;
	}
	
	public void setgId(int gId) {
		this.gId = gId;
	}
	
	public int getOrder() {
		return order;
	}
	
	public void setOrder(int order) {
		this.order = order;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public Date getGmtCreate() {
		return gmtCreate;
	}
	
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}
	
	public Date getGmtModified() {
		return gmtModified;
	}
	
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}
	
	public List<Pair> getProperties() {
		return properties;
	}
	
	public void setProperties(List<Pair> properties) {
		this.properties = properties;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[\n\tid: ");
		builder.append(id);
		builder.append("\n\tgId: ");
		builder.append(gId);
		builder.append("\n\torder: ");
		builder.append(order);
		builder.append("\n\tname: ");
		builder.append(name);
		builder.append("\n\tdescription: ");
		builder.append(description);
		builder.append("\n\tvalue: ");
		builder.append(value);
		builder.append("\n\tgmtCreate: ");
		builder.append(gmtCreate);
		builder.append("\n\tgmtModified: ");
		builder.append(gmtModified);
		
		if (null != properties && !properties.isEmpty()) {
			builder.append("\n\tproperties:\n\t{");
			for (Pair pair : properties) {
				builder.append(pair);
			}
			builder.append("}");
		}
		
		builder.append("\n]");
		return builder.toString();
	}
	
	@Override
	public int hashCode() {
		if (hashCode != 0 && hashCode != -1) {
			return hashCode;
		}
		HashFunction hf = Hashing.md5();
		HashCode hc = hf.newHasher().putInt(gId).putInt(order)
				.putString(Utils.stringToString(name)).putString(Utils.stringToString(value))
				.putString(Utils.stringToString(description)).hash();
		hashCode = hc.asInt();
		return hashCode;
	}
	
}
